package com.zprmb.udzt.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zprmb.udzt.Back_money;
import com.zprmb.udzt.R;
import com.zprmb.udzt.bean.RegularBean;
import com.zprmb.udzt.util.Regular;

public class Regular_Adapter2 extends BaseAdapter {
	private List<RegularBean> list;
	private Context context;
	private String title;
	DecimalFormat df = new DecimalFormat("###0.00");
	
	public Regular_Adapter2(List<RegularBean> list, Context context,
			String title) {
		super();
		this.list = list;
		this.context = context;
		this.title = title;
	}



	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Help h=null;
		if(convertView==null){
			h=new Help();
			convertView=LayoutInflater.from(context).inflate(R.layout.regular_item2, null);
			h.invest_info=(TextView)convertView.findViewById(R.id.invest_info);
			h.product_info=(TextView)convertView.findViewById(R.id.product_info);
			h.tv_conduct_addrate=(TextView)convertView.findViewById(R.id.conduct_addrate);
			h.tv_out_money=(TextView)convertView.findViewById(R.id.ying_outmoney);
			h.tv_conduct_addrate=(TextView)convertView.findViewById(R.id.conduct_addrate);
			h.tv_amount=(TextView)convertView.findViewById(R.id.amount);
			h.product_name=(TextView)convertView.findViewById(R.id.conduct_name);
			h.tv_back_plan=(TextView)convertView.findViewById(R.id.out_money);
			h.out_type=(TextView)convertView.findViewById(R.id.out_type);
			
			convertView.setTag(h);
		}else{
			h=(Help) convertView.getTag();
		}
		if(title.equals("已回款")){
			h.out_type.setText("回款金额(元)");
		}else{
			h.out_type.setText("应回款金额(元)");
		}
		if(Double.parseDouble(list.get(position).getRaisingRates())>0){
			h.tv_conduct_addrate.setVisibility(View.VISIBLE);
			h.tv_conduct_addrate.setText("券加息"+list.get(position).getRaisingRates()+"%");
		}else{
			h.tv_conduct_addrate.setVisibility(View.GONE);
		}
		h.tv_amount.setText(df.format(Double.parseDouble(list.get(position).getAmount())));
		if(list.get(position).getRepayAmount()==null){
			h.tv_out_money.setText("空了");
		}else{
			
			h.tv_out_money.setText(df.format(Double.parseDouble(list.get(position).getRepayAmount())));
		}
		h.invest_info.setText("投资信息:"+Regular.getMilliToDate3(list.get(position).getInvestTime())+"投资/"+Regular.getMilliToDate3(list.get(position).getDueTime())+"到期");
		h.product_info.setText("项目信息:"+"预期年化收益率"+df.format(Double.parseDouble(list.get(position).getRate()))+"%/"+list.get(position).getLoanPeriod()+"/"+list.get(position).getRepayType());
		h.product_name.setText(list.get(position).getProductName());
		if(Double.parseDouble(list.get(position).getRaisingRates())>0){
			h.tv_conduct_addrate.setVisibility(View.VISIBLE);
			h.tv_conduct_addrate.setText("券加息"+list.get(position).getRaisingRates()+"%");
		}else{
			h.tv_conduct_addrate.setVisibility(View.GONE);
		}
        h.tv_back_plan.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(context,Back_money.class);
				intent.putExtra("investId", list.get(position).getInvestId());
				intent.putExtra("type", 1);
				context.startActivity(intent);
//				if(list.get(position).getPtype().equals("claim")){
//					Intent intent=new Intent(context,InvestJiluActivity.class);
//					intent.putExtra("ptype", list.get(position).getPtype());					
//					intent.putExtra("id", list.get(position).getInvestId());
//					context.startActivity(intent);
//					
//				}else if(list.get(position).getPtype().equals("uplan")){
//					Intent intent=new Intent(context,InvestOldJiluActivity.class);
//					intent.putExtra("ptype", list.get(position).getProductType());
//					intent.putExtra("type", "UPLAN");
//					intent.putExtra("id", list.get(position).getInvestId());
//							Log.e("pype", list.get(position).getType());	
//					context.startActivity(intent);
//				}else if(list.get(position).getPtype().equals("invest")){
//					Intent intent=new Intent(context,InvestOldJiluActivity.class);
//					intent.putExtra("ptype", list.get(position).getPtype());
//					intent.putExtra("id", list.get(position).getInvestId());
//					
//					intent.putExtra("type", "BBT");
//					intent.putExtra("loanTitle", list.get(position).getProductName());
//					
//					context.startActivity(intent);
//				}
			}
		});
		return convertView;
	}
	class Help{
		private TextView invest_info,product_info,tv_conduct_addrate,tv_out_money,tv_amount,product_name,tv_back_plan,out_type;
		
	}

}
